﻿using OASystem.BLL.IService;
using OASystem.Common;
using OASystem.DAL;
using OASystem.Model;
using OASystem.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OASystem.BLL
{
    public class UserInfoService : BaseRepository<Userinfo>, IUserInfoService
    {

        #region 登陆操作
        /// <summary>
        /// 查询登录
        /// </summary>
        /// <param name="users"></param>
        /// <returns></returns>
        public ResponseResult AdminByReader(Userinfo users)
        {
            //使用MD5加密
            var md5 = MD5Encrypt.Encrypt(users.Password);
            ResponseResult responseResult = new ResponseResult();
            responseResult.data = Query(a => a.UserName == users.UserName && a.Password == md5);
            return responseResult;
        }
        #endregion

       

        #region 修改密码
        /// <summary>
        /// 从数据库查询用户密码并且进行修改操作
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResponseResult SelectbyUpdate(Userinfo users, string newpwd)
        {
            //实例化ResponseResult对象
            ResponseResult responseResult = new ResponseResult();
            //默认false
            responseResult.success = false;

            Userinfo user = new Userinfo();
            var db = new OasystemEntities1();
            //找到当前用户的ID在进行修改
            user = db.Userinfo.Find(users.UserId);
            //原密码的加密
            var md5 = MD5Encrypt.Encrypt(users.Password);
            //判断数据库加密的密码和页面输入的原密码是否一致
            if (md5 != user.Password)
            {
                responseResult.success = false;
            }
            else
            {
                //给新密码使用MD5加密
                user.Password = MD5Encrypt.Encrypt(newpwd);
                //覆盖原密码
                db.Entry(user);
                db.SaveChanges();
                responseResult.data = Query(a => a.Password == user.Password);
                //拿MD5和数据库密码比较
                if (responseResult.data != null)
                {
                    responseResult.success = true;
                }
            }
            return responseResult;
        } 
        #endregion

    }
}
